package com.easy.goods.controller;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.bean.Category;
import com.easy.bean.Products;
import com.easy.common.CommonResult;
import com.easy.goods.service.CategoryService;

@RestController 
@RequestMapping("goods")
public class CategoryController {
	@Autowired
	CategoryService categoryService;

	@RequestMapping(method = RequestMethod.GET, value = "category")
	public CommonResult getSuperCategoryList() {
		return CommonResult.success(categoryService.getSuperCategoryList());

	}
	@RequestMapping(method = RequestMethod.GET, value = "categorychildren/{parentid}")
	public CommonResult getSuperCategoryList(@PathVariable int parentid) {
		return CommonResult.success(categoryService.getCategoryChildrenList(parentid));

	}

	

	// 分页
	@RequestMapping(method = RequestMethod.GET, value = "getcategorylist")
	public CommonResult getCategoryList(Page page, Category category) {
		return CommonResult.success(categoryService.getCategoryList(page, category));

	}

	@RequestMapping(method = RequestMethod.GET, value = "getcategoryaa")
	public CommonResult getproduct(Products products) {
		List<Category> productsList = categoryService.list();
		return CommonResult.success(productsList);
	}

	@PostMapping("postcategory")
	public CommonResult postproduct(@RequestBody Category category) {
		return CommonResult.success(categoryService.save(category));
	}

	@PutMapping("updatecategory")
	public CommonResult updateproduct(@RequestBody Category category) {
		return CommonResult.success(categoryService.updateById(category));
	}

	@DeleteMapping("deletecategory/{id}")
	public CommonResult deleteproduct(@PathVariable Integer id) {

		return CommonResult.success(categoryService.deleteCategory(id));
//        return CommonResult.success(productService.deleteproduct(id));
	}

	// @GetMapping("getcoreproduct")
//    public CommonResult getcoreproduct(@RequestBody Page ,Products products) {
//        Page<Products>  productsPage=new Page<>(page.getPage(),page.getSize());
//        return CommonResult.success(productService.page(page));
//    }
	@GetMapping("categorypage")
	public CommonResult selectPage(Page page, Category category) {
		System.out.println(page.getSize() + "-----999" + category.getName() + "111------" + page.getCurrent());

		List list = categoryService.list(page, new QueryWrapper<Category>().like("name", category.getName()));
		long b = categoryService.count();
		System.out.println("总条数" + b);
		System.out.println("--------");
		int a = list.size();
		HashMap map = new HashMap();
		map.put("count", b);
		map.put("list", list);
		return CommonResult.success(map);
	}

	@PutMapping("categoryshangxiajia/{id}")
	public void shangxiajia(@PathVariable Integer id) {
		categoryService.updatecateshangxiajia(id);
	}

}
